package com.lanxiaoye.shoppingSystem.service.impl;


import com.lanxiaoye.shoppingSystem.common.exception.BaseException;
import com.lanxiaoye.shoppingSystem.common.exception.EmptyException;
import com.lanxiaoye.shoppingSystem.common.page.HistoryPage;
import com.lanxiaoye.shoppingSystem.common.page.Page;
import com.lanxiaoye.shoppingSystem.dao.HomeIndexMapper;
import com.lanxiaoye.shoppingSystem.entity.MessageActivity;
import com.lanxiaoye.shoppingSystem.entity.OwnEntity;
import com.lanxiaoye.shoppingSystem.entity.User;
import com.lanxiaoye.shoppingSystem.service.HomeIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by libing on 2016/12/13.
 */
@Service
public class HomeIndexServiceImpl implements HomeIndexService {


    @Autowired
    private HomeIndexMapper homeIndexMapper;

    @Override
    public User getUserInfo(User user) throws BaseException,EmptyException {
        try {
            if (user == null)
                throw new EmptyException("[HomeIndexServiceImpl][getUserInfo] phone EmptyException");
            return homeIndexMapper.getUserInfo(user);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getUserInfo] exception");
        }
    }

    @Override
    public MessageActivity getMessageActivity(User user) throws BaseException,EmptyException {
        try {
            if (user== null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageActivity] phone EmptyException");
            MessageActivity messageActivity = homeIndexMapper.getMessageActivity(user);
            if (messageActivity == null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageActivity] messageActivity empty");
            if (messageActivity.getMessage().getMessage_from_user_id() == null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageActivity] getMessage_from_user_id empty");
            User from_user = homeIndexMapper.getUser(messageActivity.getMessage().getMessage_from_user_id());
            if (from_user == null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageActivity] from_user empty");
            if (messageActivity.getMessage().getMessage_to_user_id() == null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageActivity] getMessage_to_user_id empty");
            User to_user = homeIndexMapper.getUser(messageActivity.getMessage().getMessage_to_user_id());
            if (to_user == null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageActivity] to_user empty");
            messageActivity.getMessage().setFrom_user(from_user);
            messageActivity.getMessage().setTo_user(to_user);
            return messageActivity;
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getMessageActivity] exception");
        }
    }

    @Override
    public int getMessageNum(User user) throws BaseException,EmptyException {
        try {
            if (user== null)
                throw new EmptyException("[HomeIndexServiceImpl][getMessageNum] phone EmptyException");
            return homeIndexMapper.getMessageNum(user);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getMessageNum] exception");
        }
    }

    @Override
    public int getSearchHistory(User user) throws BaseException,EmptyException {
        try {
            if (user == null)
                throw new EmptyException("[HomeIndexServiceImpl][getSearchHistory] phone EmptyException");
            return homeIndexMapper.getSearchHistory(user);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getSearchHistory] exception");
        }
    }

    @Override
    public List<String> getMostLikeSearch(HistoryPage page) throws BaseException,EmptyException {
        try {
            if (page.getUser_phone() == null)
                throw new EmptyException("[HomeIndexServiceImpl][getMostLikeSearch] phone EmptyException");
            return homeIndexMapper.getMostLikeSearch(page);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getMostLikeSearch] exception");
        }
    }

    @Override
    public OwnEntity getOwnEntity(String history) throws BaseException,EmptyException {
        try {
            if (history == null)
                throw new EmptyException("[HomeIndexServiceImpl][getOwnEntity] history EmptyException");
            return homeIndexMapper.getOwnEntity("%" + history + "%");
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getOwnEntity] exception");
        }
    }

    @Override
    public List<OwnEntity> getOwnEntityList(Page page) throws BaseException,EmptyException {
        try {
            if (page == null)
                throw new EmptyException("[HomeIndexServiceImpl][getOwnEntityList] page EmptyException");
            return homeIndexMapper.getOwnEntityList(page);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getOwnEntityList] exception");
        }
    }

    @Override
    public int getOwnNumbers(Page page) throws BaseException,EmptyException {
        try {
            if (page == null)
                throw new EmptyException("[HomeIndexServiceImpl][getOwnNumbers] page EmptyException");
            return homeIndexMapper.getOwnNumbers(page);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeIndexServiceImpl][getOwnNumbers] exception");
        }
    }
}
